#include "stdafx.h"
#include "PacManForm.h"

using namespace pac_man_cll;
using namespace System::Drawing;
using namespace System::Drawing::Drawing2D;

PacManForm::PacManForm(void)
{
	currentDir = MapDirection::No;
	proposedDirection = MapDirection::No;
	mouthOpen = false;
	lowerMouth = 30;
	upperMouth = 300;
	MobileForm::posInArray = gcnew array<int, 1>{-1, -1};
	MobileForm::entityId = "pacmanID";
	entitySpeed = 3;
	totalPoint = 0;
	MapTools^ tempMap = gcnew MapTools();
	pacmanMap = tempMap->myMap;
	isSpecial = hasChanged = false;
}


void pac_man_cll::PacManForm::DrawForm(Graphics^ g)
{
	mainPac = gcnew SolidBrush(Color::Yellow);
	sbfond = gcnew SolidBrush(Color::Black);
	array <int, 1>^ x_y = gcnew array <int, 1>{0, 0};
	// is pacMan freshly initiated
	if (posInArray[0] == -1)
		oldPosInPanel = posInPanel = MapTools::GetPanelPosition(gcnew array<int, 1>{ 9, 14} );
	posInArray = MapTools::GetArrayPosition(posInPanel);
	if(!mouthOpen)
		if(lowerMouth == 0)
			mouthOpen = true;
		else
		{
			lowerMouth--;
			upperMouth += 2;
		}
	else
		if (lowerMouth == 30)
			mouthOpen = false;
		else
		{
			lowerMouth++;
			upperMouth -= 2;
		}
		g->FillRectangle(sbfond, oldPosInPanel[0], oldPosInPanel[1], 30, 30);
		g->FillPie(mainPac, posInPanel[0] + 4, posInPanel[1] + 4, 22, 22, lowerMouth, upperMouth);
}



bool pac_man_cll::PacManForm::IsOneCase(void)
{
	bool thisCase = false;
	OneCase = false;
	if (posInPanel[0] % 30 == 0 && posInPanel[1] % 30 == 0)
	{
		OneCase = true;
		thisCase = true;
	}

	return thisCase;
}


bool pac_man_cll::PacManForm::IsEntityMove(void)
{
	bool moveSuccess = false;
	PacManForm^ tempForm = gcnew PacManForm();
	tempForm->posInPanel = gcnew array<int, 1>{0,0};
	tempForm->posInPanel[0] = posInPanel[0];
	tempForm->posInPanel[1] = posInPanel[1];
	
	switch (proposedDirection)
	{
	case MapDirection::Top:
		if (proposedDirection != MapDirection::No)
		{
			if (IsOneCase())
			{
				tempForm->posInPanel[1] = tempForm->posInPanel[1] - 1;
				array <int, 1>^ arrayIndex = MapTools::GetArrayPosition(tempForm->posInPanel);
				if (pacmanMap[arrayIndex[1], arrayIndex[0]] != 0 )
				{
					if (pacmanMap[arrayIndex[1], arrayIndex[0]] == 2)
					{
						pacmanMap[arrayIndex[1], arrayIndex[0]] = 1;
						totalPoint += 10;
					}
					EntityMoveHub(proposedDirection);
				}
				else
					currentDir = MapDirection::No;
			}
			else if (currentDir == MapDirection::Top || currentDir == MapDirection::Bottom)
				EntityMoveHub(proposedDirection);
			else if (currentDir != MapDirection::No)
				EntityMoveHub(currentDir);
		}
		break;
	case MapDirection::Right:
		if (proposedDirection != MapDirection::No)
		{
			if (IsOneCase())
			{
				tempForm->posInPanel[0] = tempForm->posInPanel[0] + 31;
				array <int, 1>^ arrayIndex = MapTools::GetArrayPosition(tempForm->posInPanel);
				if (pacmanMap[arrayIndex[1], arrayIndex[0]] != 0 )
				{
					if (pacmanMap[arrayIndex[1], arrayIndex[0]] == 2)
					{
						pacmanMap[arrayIndex[1], arrayIndex[0]] = 1;
						totalPoint += 10;
					}
					EntityMoveHub(proposedDirection);
				}
				else
					currentDir = MapDirection::No;
			}
			else if (currentDir == MapDirection::Right || currentDir == MapDirection::Left)
				EntityMoveHub(proposedDirection);
			else if (currentDir != MapDirection::No)
				EntityMoveHub(currentDir);
		}
		break;
	case MapDirection::Bottom:
		if (proposedDirection != MapDirection::No)
		{
			if (IsOneCase())
			{
				tempForm->posInPanel[1] = tempForm->posInPanel[1] + 31;
				array <int, 1>^ arrayIndex = MapTools::GetArrayPosition(tempForm->posInPanel);
				if (pacmanMap[arrayIndex[1], arrayIndex[0]] != 0 )
				{
					if (pacmanMap[arrayIndex[1], arrayIndex[0]] == 2)
					{
						pacmanMap[arrayIndex[1], arrayIndex[0]] = 1;
						totalPoint += 10;
					}
					EntityMoveHub(proposedDirection);
				}
				else
					currentDir = MapDirection::No;
			}
			else if (currentDir == MapDirection::Top || currentDir == MapDirection::Bottom)
				EntityMoveHub(proposedDirection);
			else if (currentDir != MapDirection::No)
				EntityMoveHub(currentDir);
		}
		break;
	case MapDirection::Left:
		if (proposedDirection != MapDirection::No)
		{
			if (IsOneCase())
			{
				tempForm->posInPanel[0] = tempForm->posInPanel[0] - 1;
				array <int, 1>^ arrayIndex = MapTools::GetArrayPosition(tempForm->posInPanel);
				if (pacmanMap[arrayIndex[1], arrayIndex[0]] != 0 )
				{
					System::DateTime^ seed = System::DateTime::Now;
					Random^ theSpecialThing = gcnew Random(seed->Millisecond);
					int ahOUi = theSpecialThing->Next(30);
					if (ahOUi == 20 && pacmanMap[arrayIndex[1], arrayIndex[0]] == 2)
					{
						isSpecial = true;
						MapTools^ tempMap = gcnew MapTools();
						if (hasChanged == false)
							pacmanMap = tempMap->specialMap;
						else
							pacmanMap = tempMap->myMap;
						hasChanged = !hasChanged;
					}
					else
					{
						if (pacmanMap[arrayIndex[1], arrayIndex[0]] == 2)
						{
							pacmanMap[arrayIndex[1], arrayIndex[0]] = 1;
							totalPoint += 10;
						}
					EntityMoveHub(proposedDirection);
					}
				}
				else
					currentDir = MapDirection::No;
			}
			else if (currentDir == MapDirection::Right || currentDir == MapDirection::Left)
				EntityMoveHub(proposedDirection);
			else if (currentDir != MapDirection::No)
				EntityMoveHub(currentDir);
		}
		break;
	case MapDirection::No:
		if (currentDir != MapDirection::No)
			System::Windows::Forms::MessageBox::Show("No");
		break;
	default:
		break;
	}
	return moveSuccess;
}


bool pac_man_cll::PacManForm::EntityMoveTop()
{
	oldPosInPanel = this->posInPanel;
	posInPanel[1] -= entitySpeed;
	currentDir = MapDirection::Top;
	return true;
}


bool pac_man_cll::PacManForm::EntityMoveRight()
{
	oldPosInPanel = this->posInPanel;
	posInPanel[0] += entitySpeed;
	currentDir = MapDirection::Right;
	return true;
}


bool pac_man_cll::PacManForm::EntityMoveBottom()
{
	oldPosInPanel = this->posInPanel;
	posInPanel[1] += entitySpeed;
	currentDir = MapDirection::Bottom;
	return true;
}


bool pac_man_cll::PacManForm::EntityMoveLeft()
{
	oldPosInPanel = this->posInPanel;
	posInPanel[0] -= entitySpeed;
	currentDir = MapDirection::Left;
	return true;
}


void pac_man_cll::PacManForm::EntityMoveHub(MapDirection dir)
{
	switch (dir)
	{
	case MapDirection::Bottom:
		EntityMoveBottom();
		break;
	case MapDirection::Left:
		EntityMoveLeft();
		break;
	case MapDirection::Top:
		EntityMoveTop();
		break;
	case MapDirection::Right:
		EntityMoveRight();
		break;
	}
}
